import { successCode } from '../../config/config'
import { cancelOrder, dataQuery } from '../../utils/apiFns'
import { showLoading, showToast, redirectTo,relaunch } from '../../utils/promiseFns'

var app = getApp();

Page({
    data: {
        asker_name: '',
        asker_phone: '',
        asker_id_num: '',
        dep_address: '',
        dest_address: '',
        bus:''
    },
    onLoad() {
        showLoading('on show')

        if (app.myorderData) {

            var id = app.myorderData.id;
            var myorderParams = {
                service: 'service',
                api: 'queryServiceRequestTravel',
                version: 1,
                params: {
                    id: id
                }
            }

            dataQuery(myorderParams)().then(res => {
                var result = res.data.result

                this.setData({
                    asker_name: result.asker_name,
                    asker_phone: result.asker_phone,
                    asker_id_num: result.asker_id_num,
                    dep_address: result.dep_address,
                    dest_address: result.dest_address,
                    bus:result.bus
                })
                wx.hideLoading()
            })
        }
    },
    onShow() {
        wx.setNavigationBarTitle({
            title: '我的出行'
        })
    },
    cancel() {

        showLoading('cancel');

        this.cancelOrder()
            .then(data => {

                return showToast('取消成功')

            })
            .then(res => {
                relaunch('/pages/index/index')
            })

    },
    arival() {

        showLoading();
        this.cancelOrder()
            .then(data => {

                return showToast('祝您旅途愉快', 'success')

            })
            .then(res => {
                relaunch('/pages/index/index')
            })

    },

    cancelOrder() {

        return new Promise((resolve, reject) => {

            var cancelParams = {
                service: 'service',
                api: 'cancelServiceRequest',
                version: 1,
                params: {
                    token: app.user_token,
                    id: app.myorderData.id
                }
            }

            dataQuery(cancelParams)().then(res => {

                if (res.statusCode == successCode) {
                    resolve(res)
                } else {
                    reject(res.msg)
                }
            })
                .catch(err => {
                    reject(err.toString())
                })

        })

    }
})